const wx = require('weixin-js-sdk')
import { mapGetters } from 'vuex'
import apiShare from '@/common/api/apiShare'
const linkUrl = process.env.VUE_APP_LINK
export default {
  computed: {
    ...mapGetters(['userInfo']),
  },
  data() {
    return {
      shareImg: require('@/common/imgs/share.png'),
    }
  },
  methods: {
    setShare() {
      const shareInfo = {
        title: '蔚来校园盲盒上线！100%中奖！9.9开盲盒，最低价值10元起',
        desc: '蔚来校园盲盒上线！100%中奖！9.9开盲盒，最低价值10元',
        // link:'http://box.tommmy.top/home',
        // link:'http://box.sdxgxx.cn/home',
        link: linkUrl,
        img: 'http://alivod.sdxgxx.cn/image/default/74EE3F456FFD466AB9C0546D8B0E69E5-6-2.png',
      }
      this.wechatShare(shareInfo)
    },
    wechatShare(info) {
      // // 判断苹果手机
      // let _url = ''
      // if (window.__wxjs_is_wkwebview === true) {
      //   _url = window.location.href.split('#')[0] || window.location.href
      // } else {
      //   _url = window.location.href
      // }
      // 访问后台接口获取微信参数
      this.$toast.loading()
      apiShare
        .getShareInfo({
          userId: this.userInfo.userId,
          url: location.href.split('#')[0], //'http://box.tommmy.top/home',
        })
        .then((res) => {
          this.$toast.clear()
          this.setShareWxInfo(info, res.data)
        })
    },
    setShareWxInfo(info, data) {
      wx.config({
        debug: false,
        appId: data.appId, // 必填，公众号的唯一标识
        timestamp: data.timestamp, // 必填，生成签名的时间戳
        nonceStr: data.nonceStr, // 必填，生成签名的随机串
        signature: data.signature, // 必填，签名，见附录1
        jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData'],
      })

      wx.checkJsApi({
        jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData'],
        success: function() {
          console.log('success')
        },
        fail: function() {
          console.log('err')
        },
      })
      wx.ready(() => {
        const share_title = info.title
        const share_desc = info.desc
        const share_link = info.link
        const share_img = info.img
        wx.showOptionMenu()
        wx.updateAppMessageShareData({
          title: share_title, // 分享标题
          desc: share_desc, //
          link: share_link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
          imgUrl: share_img, // 分享图标
          success: function() {
            console.log('分享给朋友')
          },
          cancel: function() {
            console.log('已取消分享')
          },
        })
        wx.updateTimelineShareData({
          title: share_title, // 分享标题
          desc: share_desc, //
          link: share_link, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
          imgUrl: share_img, // 分享图标
          success: function() {
            console.log('分享到朋友圈')
          },
          cancel: function() {
            console.log('已取消分享')
          },
        })
      })
      wx.error((err) => {
        that.$toast.err(err)
      })
    },
  },
}
